package com.qgzj.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qgzj.common.constants.Constant;
import com.qgzj.common.utils.RandomIDUtils;
import com.qgzj.common.vo.ContentRequest;
import com.qgzj.entity.Content;
import com.qgzj.mapper.ContentMapper;
import com.qgzj.response.PageQueryRes;
import com.qgzj.util.ApplicationUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ContentService {

    @Resource
    ContentMapper contentMapper;


    private Logger logger = LoggerFactory.getLogger(ContentService.class);

    //查询centent表中 picUrl为不为空的 staus为0的 1000条数据
    public List<Content> getPicUrlFromContent() {
        EntityWrapper<Content> wrapper = new EntityWrapper<>();
        wrapper.eq("status", Constant.StudyContentConstant.status_文本未识别);
        wrapper.isNotNull("pic_url");
        wrapper.ne("pic_url", "");
        wrapper.last("limit 1000");
        List<Content> contents = contentMapper.selectList(wrapper);
        return contents;
    }

    public void updateContentText(long contentId, String text) {
        Content content = new Content();
        content.setId(contentId);
        content.setContentText(text);
        //文本被识别 状态为1
        content.setStatus(Constant.StudyContentConstant.status_文本已被ocr识别);
        content.setUpdateTime(new Date());
        EntityWrapper<Content> wrapper = new EntityWrapper<>();
        wrapper.eq("id", contentId);
        Integer update = contentMapper.update(content, wrapper);
    }

    public PageQueryRes<Content> selectContent(ContentRequest contentReq) {
        int page = contentReq.getPage();
        int pageSize = contentReq.getPageSize();
        PageQueryRes res = new PageQueryRes();
        res.setPage(page);
        res.setPageSize(pageSize);
        String materialNo = contentReq.getMaterialNo();
        String volumeNo = contentReq.getVolumeNo();
        String contentNo = contentReq.getContentNo();
        EntityWrapper<Content> wrapper = new EntityWrapper<>();
        wrapper.eq("material_no", materialNo);
        wrapper.eq("volume_no", volumeNo);
        wrapper.eq("content_no", contentNo);
        wrapper.orderBy("id",false);
        List<Content> contents = contentMapper.selectPage(new Page<>(page, pageSize),wrapper);
        res.setCount(contentMapper.selectCount(wrapper));
        res.setList(contents);
        return res;
    }


    public void insertContent(ContentRequest contentReq) {
        Content content = transContentReq2Content(contentReq);
        content.setCreateTime(new Date());
        content.setUpdateTime(new Date());
        if(StringUtils.isBlank(content.getContentNo())){
            content.setContentNo(ApplicationUtil.getCommonNo());
        }
        Integer insert = contentMapper.insert(content);
    }

    public void updateContent(ContentRequest contentReq) {
        Content content = transContentReq2Content(contentReq);
        content.setUpdateTime(new Date());
        String volumeNo = content.getVolumeNo();
        String materialNo = content.getMaterialNo();
        String contentNo = content.getContentNo();
        EntityWrapper<Content> wrapper = new EntityWrapper<>();
        wrapper.eq("volume_no", volumeNo);
        wrapper.eq("material_no", materialNo);
        wrapper.eq("content_no", contentNo);
        Integer update = contentMapper.update(content, wrapper);
    }

    public void deleteContent(ContentRequest contentReq) {
        String volumeNo = contentReq.getVolumeNo();
        String materialNo = contentReq.getMaterialNo();
        String contentNo = contentReq.getContentNo();
        EntityWrapper<Content> wrapper = new EntityWrapper<>();
        wrapper.eq("volume_no", volumeNo);
        wrapper.eq("material_no", materialNo);
        wrapper.eq("content_no", contentNo);
        Integer update = contentMapper.delete(wrapper);
    }

    public Content transContentReq2Content(ContentRequest contentReq){
        Content content = new Content();
//        if(StringUtils.isNotBlank(contentReq.getContentNo())){
//            content.setContentNo(contentReq.getContentNo());
//        }
//        if(null!=contentReq.getCurrentPage()){
//            content.setCurrentPage(contentReq.getCurrentPage());
//        }
//        if(StringUtils.isNotBlank(contentReq.getVolumeNo())){
//            content.setVolumeNo(contentReq.getVolumeNo());
//        }
//        if(StringUtils.isNotBlank(contentReq.getMaterialNo())){
//            content.setMaterialNo(contentReq.getMaterialNo());
//        }
//        if(StringUtils.isNotBlank(contentReq.getPicUrl())){
//            content.setPicUrl(contentReq.getPicUrl());
//        }
//        if(StringUtils.isNotBlank(contentReq.getContentText())){
//            content.setContentText(contentReq.getContentText());
//        }
//        if (null!=contentReq.getStatus()){
//            content.setStatus(contentReq.getStatus());
//        }
//        content.setCreateTime(new Date());
//        content.setUpdateTime(new Date());
        BeanUtils.copyProperties(contentReq,content);
        return content;
    }


}
